package com.inossem.warehousestorage_inossem.activity.inventory.chuangyou;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.extre.InventoryExtre;
import com.inossem.warehousestorage_inossem.adapter.inventory.chuangyou.InventoryMatsAdapter;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryMatsBean;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryMatsUpDataBean;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryMatsUpDataRequestBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.chuangyou.InventoryMatsRequest;
import com.inossem.warehousestorage_inossem.request.inventory.chuangyou.InventoryMatsUpDataRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;
import com.inossem.warehousestorage_inossem.utils.NoScrollListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 物料列表页
 *
 * @author 詹建宇
 * @time on 2018/3/31 16:29
 * @email jianyu.zhan@inossem.com
 */
public class InventoryMatsActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private NoScrollListView listView;

    private List<ItemListBean> allData;//物料列表

    private String position_id;
    private String stocktake_id;

    private InventoryMatsAdapter adapter;

    private String location_id;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_inventory_mat_list);

        initView(this);
        getExtra();
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (NoScrollListView) activity.findViewById(R.id.listView);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("物料列表");
        ActivityUtils.initBackAndFunction(activity, "保存", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                upData(activity);
            }
        });
        ActivityUtils.initRightFunction(activity, ActivityUtils.getRightViewWithName(activity, "新增"), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ItemListBean bean = new ItemListBean();
                Intent intent = new Intent(activity, InventoryMatDetailsActivity.class);
                intent.putExtra(InventoryExtre.ITEMBEAN, bean);
                intent.putExtra(InventoryExtre.LOCATION_ID, String.valueOf(location_id));
                activity.startActivity(intent);
            }
        });
        getData(activity);
    }

    private void getExtra() {
        position_id = getIntent().getStringExtra(InventoryExtre.POSITION_ID);
        stocktake_id = getIntent().getStringExtra(InventoryExtre.STOCKTAKE_ID);
    }

    private void getData(Context context) {
        InventoryMatsRequest.request(context, position_id, stocktake_id, new GsonBeanListener<InventoryMatsBean>() {
            @Override
            public void onSuccess(InventoryMatsBean bean) {
                location_id = bean.getBody().getLocation_id();
                allData = bean.getBody().getItem_list();
                for (ItemListBean itemListBean : allData) {
                    itemListBean.setIs_new("0");
                }
                addListData(context, ActivityUtils.getShowData(allData, 0), listView, bean.getBody().getStocktake_type(), true);
            }
        });

    }

    private void addListData(Context context, List<ItemListBean> list, ListView listView, String stocktake_type, boolean flag) {
        adapter = new InventoryMatsAdapter(context, list, stocktake_type, location_id, stocktake_id);
        listView.setAdapter(adapter);
    }

    private void upData(Activity activity) {
        List<EditText> editTexts = adapter.getEditTexts();
        InventoryMatsUpDataRequestBean inventoryMatsUpDataRequestBean = new InventoryMatsUpDataRequestBean();
        List<InventoryMatsUpDataRequestBean.ItemListBean> itemListBeans = new ArrayList<>();
        for (EditText editText : editTexts) {
            if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                InventoryMatsUpDataRequestBean.ItemListBean bean = new InventoryMatsUpDataRequestBean.ItemListBean();
                bean.setItem_id((String) editText.getTag());
                bean.setStocktake_qty(editText.getText().toString().trim());
                itemListBeans.add(bean);
            }
        }
        inventoryMatsUpDataRequestBean.setItem_list(itemListBeans);
        InventoryMatsUpDataRequest.request(activity, inventoryMatsUpDataRequestBean, new GsonBeanListener<InventoryMatsUpDataBean>() {
            @Override
            public void onSuccess(InventoryMatsUpDataBean bean) {
                Utils.middleToast(activity, "提交成功");
                finish();
            }
        });
    }

}
